package com.zzy.center.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzy.center.entity.UcenterMember;
import com.zzy.center.entity.vo.RegisterVo;
import com.zzy.center.mapper.UcenterMemberMapper;
import com.zzy.center.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzy.commonutils.JwtUtils;
import com.zzy.commonutils.MD5;
import com.zzy.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author KAZU
 * @since 2023-01-19
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
	
	@Autowired
	private UcenterMemberMapper memberMapper;
	
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	
	/**
	 * 登录
	 * */
	@Override
	public String login(UcenterMember member) {
		// 获取手机号喝密码
		String mobile = member.getMobile();
		String password = member.getPassword();
		// 手机号和密码非空判断
		if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
			throw new GuliException(20001, "未填写手机号或密码");
		}
		// 判断手机号是否正确
		QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
		wrapper.eq("mobile", mobile);
		UcenterMember mobilMember = memberMapper.selectOne(wrapper);
		// 判断查询对象是否为空
		if (mobilMember == null) {
			throw new GuliException(20001, "该手机号未注册");
		}
		
		// 判断密码
		if (!MD5.encrypt(password).equals(mobilMember.getPassword())) {
			throw new GuliException(20001, "密码错误");
		}
		// 判读用户是否被禁用
		if (mobilMember.getIsDisabled()) {
			throw new GuliException(20001, "Your account has been banned!");
		}
		// 用jwt生成token
		return JwtUtils.getJwtToken(mobilMember.getId(), mobilMember.getNickname());
	}
	
	/**
	 * 注册
	 * */
	@Override
	public void register(RegisterVo registerVo) {
		// 获取数据
		String code = registerVo.getCode();
		String mobile = registerVo.getMobile();
		String nickname = registerVo.getNickname();
		String password = registerVo.getPassword();
		// 非空判断
		if (StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile) ||
				StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)) {
			throw new GuliException(20001, "注册失败");
		}
		// 判断验证码
		// 获取redis验证码
		String redisCode = redisTemplate.opsForValue().get("phone" + mobile);
		if (!code.equals(redisCode)) {
			throw new GuliException(20001, "验证码不正确或已失效");
		}
		// 判断手机号是否重复，表里存在相同手机号不进行添加
		QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
		wrapper.eq("mobile", mobile);
		Integer count = memberMapper.selectCount(wrapper);
		if (count > 0) {
			throw new GuliException(20001, "该手机号已被注册，请更换手机号尝试");
		}
		// 数据添加数据库中
		UcenterMember ucenterMember = new UcenterMember();
		ucenterMember.setMobile(mobile);
		ucenterMember.setNickname(nickname);
		ucenterMember.setPassword(MD5.encrypt(password));
		ucenterMember.setIsDisabled(false);
		ucenterMember.setAvatar("https://edu-web-0105.oss-cn-hangzhou.aliyuncs.com/a.jpg");

		int insert = memberMapper.insert(ucenterMember);
		if (insert <= 0) {
			throw new GuliException(20001, "无效注册");
		}
		
	}
	
	/**
	 * 查询某一天的注册人数
	 * */
	@Override
	public Integer countRegisterDay(String day) {
		return memberMapper.countRegisterDay(day);
	}
}

















